package com.sdk.network;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class NetworkBroadcastReceiver extends BroadcastReceiver {
	public final static String BROADCAST_NETWORK_NO_AVAILABLE = "broadcast_network_no_available";

	private static boolean isNetworkAvailable;

	private static OnNetworkChangeListener mOnNetworkChangeListener;

	/**
	 * 设置网络监听器
	 * 
	 * @param aOnNetworkChangeListener
	 */
	public static void setOnNetworkChangerListener(
			OnNetworkChangeListener aOnNetworkChangeListener) {
		if (aOnNetworkChangeListener == null) {
			return;
		}
		NetworkBroadcastReceiver.mOnNetworkChangeListener = aOnNetworkChangeListener;
	}

	public interface OnNetworkChangeListener {
		void onChangeToAvailable();

		void onChangeToUnAvailable();

		void onNetworkUnAvailble();

		void onNetworkAvailable();
	}

	private long lastReceiveTime = 0;

	@Override
	public void onReceive(Context context, Intent intent) {
		final String action = intent.getAction();
		if (isShouldReceiveBroadcast(lastReceiveTime)) {
			lastReceiveTime = System.currentTimeMillis();
			if (action.equalsIgnoreCase("android.intent.action.SERVICE_STATE")) {
				isNetworkAvailable = isNetworkAvailable(context);
				if (isNetworkAvailable) {
					if (null != mOnNetworkChangeListener) {
						mOnNetworkChangeListener.onNetworkAvailable();
					}
				} else {
					if (null != mOnNetworkChangeListener) {
						mOnNetworkChangeListener.onNetworkUnAvailble();
					}
				}
				return;
			} else if (action
					.equalsIgnoreCase(ConnectivityManager.CONNECTIVITY_ACTION)
					&& !action
							.equalsIgnoreCase("android.intent.action.SERVICE_STATE")) {
				if (isNetworkAvailable(context)) {
					if (null != mOnNetworkChangeListener) {
						mOnNetworkChangeListener.onChangeToAvailable();
					}
				} else {
					if (mOnNetworkChangeListener != null) {
						mOnNetworkChangeListener.onChangeToUnAvailable();
					}
				}
			}
		}
	}

	private boolean isShouldReceiveBroadcast(long lstTime) {
		final long currentTime = System.currentTimeMillis();
		final long gapTime = currentTime - lstTime;
		if (Math.abs(gapTime) > 500) {
			return true;
		}
		return false;
	}

	private static NetworkBroadcastReceiver watcher;

	/**
	 * 注册网络监听器
	 * 
	 * @param context
	 *            上下文 <h1>注意：需注册 uses-permission
	 *            android:name="android.permission.ACCESS_NETWORK_STATE"</h1>
	 */
	public static void register(Context context) {
		synchronized (context) {
			watcher = new NetworkBroadcastReceiver();
		}
		final IntentFilter intentFilter = new IntentFilter();
		intentFilter.addAction("android.intent.action.AIRPLANE_MODE_CHANGED");
		intentFilter.addAction("android.intent.action.SERVICE_STATE");
		intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
		context.registerReceiver(watcher, intentFilter);
	}

	public static void unregister(Context context) {
		synchronized (context) {
			if (null != watcher) {
				context.unregisterReceiver(watcher);
				watcher = null;
			}
			if (null != mOnNetworkChangeListener) {
				mOnNetworkChangeListener = null;
			}
		}
	}

	/**
	 * 判断网络是否可用
	 * 
	 * @param context
	 *            上下文
	 * @return true可用
	 */
	public static boolean isNetworkAvailable(Context context) {
		final ConnectivityManager connectivity = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		if (connectivity == null) {
		} else {
			final NetworkInfo[] info = connectivity.getAllNetworkInfo();
			if (info != null) {
				for (int i = 0; i < info.length; i++) {
					if (info[i].getState() == NetworkInfo.State.CONNECTED) {
						return true;
					}
				}
			}
		}
		return false;
	}
}
